package tom_io_nio.nio.channel;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileInputProgram {

    public static void main(String[] args){
        try{
            FileInputStream fileInputStream = new FileInputStream("/Users/mac/Desktop/test.txt");

            //获取通道
            FileChannel fileChannel = fileInputStream.getChannel();

            //创建缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);//分配容量

            //读取数据到缓冲区
            fileChannel.read(byteBuffer);

            byteBuffer.flip();

            while (byteBuffer.remaining() > 0){
                byte b = byteBuffer.get();
                System.out.println((char) b);
            }

            fileInputStream.close();
            fileChannel.close();
        }catch (Exception e){
            e.printStackTrace();
        }


    }
}
